package cn.edu.cdtu.healthserver.servlet;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.cdtu.healthserver.domain.HealthFileBody;
import cn.edu.cdtu.healthserver.domain.HealthFileMental;
import cn.edu.cdtu.healthserver.domain.UserInformation;
import cn.edu.cdtu.healthserver.service.HealthFileService;
import cn.edu.cdtu.healthserver.service.LoginControl;
import cn.edu.cdtu.healthserver.service.impl.HealthFileServiceImplementation;
import cn.edu.cdtu.healthserver.utils.ConnectUtil;

import com.google.gson.Gson;

/**
 * Servlet implementation class HealthFileServlet
 */
/**
 * @author zheng
 *
 */
@WebServlet("/HealthFileServlet")
public class HealthFileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private HealthFileService healthFileService = new HealthFileServiceImplementation();
	private Gson gson = new Gson();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public HealthFileServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		String type_id = request.getParameter("type_id");	// 判断档案类型 身体或是心理

		UserInformation user = LoginControl.getLoginedUser(request);

		if (type_id.equals("1")) {
			
			HealthFileMental fileMental = healthFileService.getHealthFileMental(user);
			
			sendFileMental(request, response, fileMental);
			
		} else if (type_id.equals("2")) {
			
			HealthFileBody fileBody = healthFileService.getHealthFileBody(user);
				
			sendFileBody(request, response, fileBody);
		}
	}

	/**
	 * 发送身体档案至客户端
	 * 上午11:11:58
	 * @param request
	 * @param response
	 * @param fileBody
	 * @throws IOException
	 * void
	 */
	public void sendFileBody(HttpServletRequest request,
			HttpServletResponse response, HealthFileBody fileBody)	
			throws IOException {

		response.setContentType(ConnectUtil.ContentTypeString.jSON_STRING);
		OutputStream writer = response.getOutputStream();
		writer.write(gson.toJson(fileBody).getBytes());
		writer.flush();
		writer.close();
	}
	
	/**
	 * 发送心理档案至客户端
	 * 上午11:12:36
	 * @param request
	 * @param response
	 * @param fileMental
	 * @throws IOException
	 * void
	 */
	public void sendFileMental(HttpServletRequest request,			
			HttpServletResponse response, HealthFileMental fileMental) throws IOException{
		response.setContentType(ConnectUtil.ContentTypeString.jSON_STRING);
		OutputStream writer = response.getOutputStream();
		writer.write(gson.toJson(fileMental).getBytes());
		writer.flush();
		writer.close();
	}

}
